package com.lintf.sharding.sphere.algorithm;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Properties;

/**
 * @author lin tingfeng
 * @system Mac
 * @data 2024/10/25 16:12
 * @package_name com.lintf.sharding.sphere.algorithm
 * @project_name sharding-sphere
 * @desc
 */
public class SysUserShardingAlgorithm implements StandardShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        String logicTableName = shardingValue.getLogicTableName();
        Long value = shardingValue.getValue();
        int tableNum = (int) (value % 3);
        String tableName = logicTableName + "_" + tableNum;
        if (availableTargetNames.contains(tableName)) {
            return tableName;
        }
        throw new RuntimeException("No valid table found for sharding value: " + value);
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        return availableTargetNames;
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}
